﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JsalesModel;

namespace JSales
{
    public class CheckRight
    {
        public static bool CheckModel(long cUser, long Model)
        {
            bool result = false;
            using (JYEntities dbContext = new JYEntities())
            {
                IEnumerable<TbRight> serverRights = from i in dbContext.TbRights
                                                    where i.UserID == cUser
                                                    select i;
                if (serverRights.Any())
                {
                    int roleid = serverRights.FirstOrDefault().RoleID.Value;

                    IEnumerable<TbRole> roles = from i in dbContext.TbRoles
                                                where i.RoleName == roleid.ToString() && i.Model == Model
                                                select i;
                    if (roles.Any())
                    {
                        result = true;
                    }
                }
            }
            return result;
        }

        public static bool Check(long cUser, long Model, GlobalVar.RightType type)
        {
            bool result = false;
            using (JYEntities dbContext = new JYEntities())
            {
                IEnumerable<TbRight> serverRights = from i in dbContext.TbRights
                                                    where i.UserID == cUser
                                                    select i;
                if (serverRights.Any())
                {
                    int roleid = serverRights.FirstOrDefault().RoleID.Value;

                    IEnumerable<TbRole> roles = from i in dbContext.TbRoles
                                                where i.RoleName == roleid.ToString() && i.Model == Model
                                                select i;
                    if (roles.Any())
                    {
                        switch (type)
                        {
                            case GlobalVar.RightType.Add:
                                {
                                    var a = from i in roles
                                            select i.Addition;
                                    if (a.Any())
                                    {
                                        result = a.Contains(true);
                                    }
                                }
                                break;
                            case GlobalVar.RightType.Delete:
                                {
                                    var a = from i in roles
                                            select i.Delete;
                                    if (a.FirstOrDefault().Value)
                                        result = true;
                                }
                                break;
                            case GlobalVar.RightType.Edit:
                                {
                                    var a = from i in roles
                                            select i.Edit;
                                    if (a.FirstOrDefault().Value)
                                        result = true;
                                }
                                break;
                            case GlobalVar.RightType.Query:
                                {
                                    var a = from i in roles
                                            select i.Query;
                                    if (a.FirstOrDefault().Value)
                                        result = true;
                                }
                                break;
                            case GlobalVar.RightType.Save:
                                {
                                    var a = from i in roles
                                            select i.Saves;
                                    if (a.FirstOrDefault().Value)
                                        result = true;
                                }
                                break;
                            case GlobalVar.RightType.Commit:
                                {
                                    var a = from i in roles
                                            select i.Commits;
                                    if (a.FirstOrDefault().Value)
                                        result = true;
                                }
                                break;
                            case GlobalVar.RightType.Approval:
                                {
                                    var a = from i in roles
                                            select i.Approval;
                                    if (a.FirstOrDefault().Value)
                                        result = true;
                                }
                                break;
                            case GlobalVar.RightType.Reject:
                                {
                                    var a = from i in roles
                                            select i.Reject;
                                    if (a.FirstOrDefault().Value)
                                        result = true;
                                }
                                break;
                        }
                    }
                }
            }
            return result;
        }
    }
}
